package com.example.shirotest.console;

import com.example.shirotest.entry.Result;
import com.example.shirotest.entry.Sites;
import com.example.shirotest.service.ResultService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

@Component
public class LoadHtml {

    @Autowired
    private ResultService resultService;
    @Autowired
    private ShareData shareData;

    public void getHref(Sites sites){
        String urllink = sites.getUrl();
        String regex   = sites.getRegex();

        URL urlobj = null;
        String hosturl = "";
        String hostpro = "";
        try {
            urlobj = new URL(urllink);
            hosturl = urlobj.getProtocol() + "://" + urlobj.getHost();
            hostpro = urlobj.getProtocol();
        } catch (MalformedURLException e) {
            //throw new RuntimeException(e);
            System.out.println(e.getMessage());
            return;
        }

        try {
            Document document = Jsoup.connect(urllink).get();

            Elements a = document.select(regex).select("a");

            ArrayList<Result> results = new ArrayList<>();
            for (Element element : a) {
                if(element.text().isEmpty())continue;
                Result result = new Result();
                result.setTitle(element.select("a").text());

                //链接判断，规范化处理
                String href = element.select("a").attr("href");
                if(!href.matches("^http.*")){
                    //相对当前连接的地址
                    if(href.matches("^\\./.*")){
                        href = urllink + href.substring(2);
                    }

                    //以//为开始的地址
                    if(href.matches("^//.*")){
                        href = hostpro + ":" + href;
                    }

                    //相对域名的地址
                    if(href.matches("^/.*")){
                        href = hosturl + href;
                    }
                }

                //数据判断去重
                if(!shareData.isUnique(sites.getId(),href))continue;
                result.setUrl(href);
                result.setSid(sites.getId());
                results.add(result);
            }

            if(results.size()>0){
                resultService.saveBatch(results);
            }

        } catch (IOException e) {
            System.out.println(e.getMessage());
            return;
        }

    }

}
